<?php
/**
 * Created by PhpStorm.
 * User: 李迪
 * Date: 2019/2/25
 * Time: 15:59
 */
namespace App\Http\Controllers\Homepage;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseApiController;
use App\Services\Homepage\ActivityService;
use App\Services\Homepage\EventParticipants;
class ActivityController extends BaseApiController
{
    private $Activity;
    private $eventParticipants;

    public function __construct(ActivityService $Activity,EventParticipants $eventParticipants)
    {
        parent::__construct();
        $this->Activity = $Activity;
        $this->eventParticipants = $eventParticipants;
    }
    
    //查询所有活动
    public function getAll()
    {
        $arr = $this->Activity->activityList();
        return json_encode($arr);
    }
    //活动详情
    public function find(Request $request){
        $data['id'] = $request->post('id');
        $val = $this->idFind($data);
        if($val->fails()){
            return $val->errors();
        }
        return $this->Activity->find($data['id']);
    }
    //活动报名
    public function singUp(Request $request){

        $rule = [
            'a_id'=>'required|integer',
            'name'=>'required',
            'contact'=>'required',
        ];
        $tips = [
            'a_id.required'=>'活动id不能为空',
            'a_id.integer'=>'活动id类型不对',
            'name.required'=>'用户名不能为空',
            'contact.required'=>'联系方式不能为空'
        ];
        $val = $this->validator($request->all(),$rule,$tips);
        if($val->fails()){
            return $val->errors();
        }
        $data=  $request->all();
        if( $this->eventParticipants->signUp($data)){
            return $this->result('200','报名成功','success');
        }else{
            return $this->result('20001','报名失败','errors');
        }
    }
}